package com.voute.View.Activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.afollestad.materialdialogs.MaterialDialog;
import com.voute.App.App;
import com.voute.Base.BaseActivity;
import com.voute.Model.Bean.SimpleCircleInfoBean;
import com.voute.Model.Bean.SimpleCircleInfoEntity;
import com.voute.Presenter.ISimpleCirclePresenter;
import com.voute.Presenter.Impl.SimpleCirclePresenterImpl;
import com.voute.R;
import com.voute.View.Adapter.SimpleCircleInfoAdapter;
import com.voute.View.IView.ICirclesListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xianke on 22/04/2017.
 */

public class CirclesListActivity extends BaseActivity implements ICirclesListView {

    private RelativeLayout back_layout;
    private SwipeRefreshLayout swipe_refresh_layout;
    private ListView content_list;
    private List<SimpleCircleInfoEntity> datas = new ArrayList<>();
    private SimpleCircleInfoAdapter adapter;

    private MaterialDialog progressDialog;

    private ISimpleCirclePresenter presenter;
    private boolean isRefresh = true;

    private int page = 1;
    private boolean stopLoadMore;
    private boolean isLoading;

    @Override
    public void showLoading() {
        if (progressDialog == null) {
            progressDialog = new MaterialDialog.Builder(CirclesListActivity.this)
                    .progress(true, 0)
                    .title("提示")
                    .content("请等待...")
                    .autoDismiss(false)
                    .cancelable(true)
                    .build();
        }
    }

    @Override
    public void hideLoading() {
        swipe_refresh_layout.setRefreshing(false);
        progressDialog.dismiss();
    }

    @Override
    public void showError(String errMsg) {
        swipe_refresh_layout.setRefreshing(false);
        MaterialDialog notice_dialog = new MaterialDialog.Builder(CirclesListActivity.this).title("提示").positiveText("确认").cancelable(false).content(errMsg).build();
        notice_dialog.show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        isRefresh = true;
        doRefreshSimpleCircleMsg();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_circle_list;
    }

    @Override
    public void setupView() {
        back_layout = $(R.id.back_layout);
        swipe_refresh_layout = $(R.id.swipe_refresh_layout);
        content_list = $(R.id.content_list);
        back_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                App.GetInstance().finishActivity(CirclesListActivity.this);
            }
        });
        adapter=new SimpleCircleInfoAdapter(datas,this);
        content_list.setAdapter(adapter);
        presenter = new SimpleCirclePresenterImpl(this);
        swipe_refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isRefresh=true;
                doRefreshSimpleCircleMsg();
            }
        });
        content_list.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {

            }

            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                int last_visiable = absListView.getLastVisiblePosition();
                if (!stopLoadMore && !isLoading && (last_visiable > 0)) {
                    if (last_visiable + 2 > totalItemCount) {
                        isRefresh = false;
                        doRefreshSimpleCircleMsg();
                    }
                }
            }
        });
    }

    private void doRefreshSimpleCircleMsg() {
        int page_local = 1;
        if (isRefresh)
            page_local = 1;
        else {
            if (datas.size() == 0) {
                page_local = 1;
            } else {
                page_local = page;
            }
        }
        if (!isRefresh) {
            if (stopLoadMore) {
                return;
            } else {
                stopLoadMore = true;
                isLoading = true;
            }
        } else {
            isLoading = true;
            stopLoadMore = true;
        }
        presenter.requestCircles(page_local+"");
    }

    @Override
    public void setupData() {

    }

    @Override
    public void refreshCirclesList(SimpleCircleInfoBean response) {
        if (response != null) {
            if (response.getStatus() == 0) {
                isLoading = false;
                if (isRefresh) {
                    page = 1;
                    datas.clear();
                    isRefresh = false;
                }
                if (response.getData() != null) {
                    if (response.getData().getCircles() != null && response.getData().getCircles().size() > 0) {
                        page=page+1;
                        datas.addAll(response.getData().getCircles());
                        adapter.setDatas(datas);
                        stopLoadMore = false;
                    } else {
                        stopLoadMore = true;
                    }
                } else {
                    isLoading = false;
                    stopLoadMore = false;
                }
            } else {
                isLoading = false;
                stopLoadMore = false;
            }
        } else {
            isLoading = false;
            stopLoadMore = false;
        }
    }
}
